package cn.mzcode.xyzpfuwuduan.service.impl;

import cn.mzcode.xyzpfuwuduan.common.ServerResponse;
import cn.mzcode.xyzpfuwuduan.dao.ColletcionMapper;
import cn.mzcode.xyzpfuwuduan.dao.PositionMapper;
import cn.mzcode.xyzpfuwuduan.pojo.Colletcion;
import cn.mzcode.xyzpfuwuduan.pojo.Position;
import cn.mzcode.xyzpfuwuduan.service.ICollectionService;
import cn.mzcode.xyzpfuwuduan.vo.CollectionVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.plugin2.gluegen.runtime.CPU;

import java.util.Collection;
import java.util.List;

@Service
public class CollectionServiceImpl implements ICollectionService {

    @Autowired
    private ColletcionMapper collectionMapper;
    @Autowired
    private PositionMapper positionMapper;


    @Override
    public ServerResponse addOrdelete(int studentId, int positionId) {

        Colletcion colletcion = collectionMapper.selectBySidAndPid(studentId, positionId);
        if (colletcion != null && colletcion.getStatus().equals("1")) {
            collectionMapper.updateStatus(studentId, positionId);
            return ServerResponse.createBySuccess("取消收藏");
        } else if (colletcion != null && colletcion.getStatus().equals("0")) {
            collectionMapper.updateStatus1(studentId, positionId);
            return ServerResponse.createBySuccess("收藏成功");
        } else {
            Colletcion colletcion1=new Colletcion();
            colletcion1.setStudentId(studentId);
            colletcion1.setPositionId(positionId);
            colletcion1.setStatus("1");
            collectionMapper.insert(colletcion1);
            return ServerResponse.createBySuccess("收藏成功");
        }



    }

    @Override
    public ServerResponse<PageInfo> getCollection(int studentId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        List<Colletcion> collectionList=collectionMapper.selectBystudentId(studentId);
        List<CollectionVo> collectionVoList=Lists.newArrayList();
        for (Colletcion collectionItem:collectionList){
            int positionId=collectionItem.getPositionId();
            Position position=positionMapper.selectByPrimaryKey(positionId);
            CollectionVo collectionVo=assembleCompanyList(position);
            collectionVoList.add(collectionVo);
        }
        PageInfo<CollectionVo> pageInfo=new PageInfo(collectionList);
        pageInfo.setList(collectionVoList);
        return ServerResponse.createBySuccess(pageInfo);

    }

    private CollectionVo assembleCompanyList(Position position){
            CollectionVo collectionVo = new CollectionVo();
            collectionVo.setPositionName(position.getPositionName());
            collectionVo.setSalary(position.getSalary());
            collectionVo.setTimes(position.getTimes());
            collectionVo.setType(position.getTypes());
            collectionVo.setWorkpalce(position.getWorkplace());
            return collectionVo;

    }


}



